package com.home.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.home.common.Cache;
import com.home.model.Zone;
import com.home.service.ZoneService;

@Controller
public class ZoneAction {
	@Autowired
	private ZoneService zoneService;

	@RequestMapping("/admin/zone/list.do")
	public String getAll(HttpServletRequest request) {
		List<Zone> zoneList = this.getZoneService().getList();
		request.setAttribute("list", zoneList);
		return "/admin/zone/list";
	}

	@RequestMapping("/admin/zone/save.do")
	public String getAll(Zone zone) {
		if (zone != null) {
			this.getZoneService().save(zone);
		}
		Cache.getInstance().refreshZone();
		return "redirect:/admin/zone/list.do";
	}

	@RequestMapping("/admin/zone/get.do")
	public String get(Integer id, HttpServletRequest request) {
		if (id != null) {
			Zone zone = this.getZoneService().getZone(id);
			request.setAttribute("obj", zone);
		}
		return "/admin/zone/obj";
	}
	
	@RequestMapping("/admin/zone/delete.do")
	public String delete(Integer id, HttpServletRequest request) {
		if (id != null) {
			Zone z=new Zone();
			z.setId(id);
			this.getZoneService().delete(z);
		}
		return "redirect:/admin/zone/list.do";
	}

	public ZoneService getZoneService() {
		return zoneService;
	}

	public void setZoneService(ZoneService zoneService) {
		this.zoneService = zoneService;
	}

}
